package elion.com.theElion;
//主界面

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;

import elion.com.elion.R;
import elion.com.theElion.activityMain.Me_AboutActivity;
import elion.com.theElion.activityMain.Me_ContectActivity;
import elion.com.theElion.fragmentLandData.SelectFragment;
import elion.com.theElion.fragmentMain.ServiceFragment;
import elion.com.theElion.fragmentMain.WarningFragment;
import elion.com.theElion.fragmentSeed.SeedActivity;
import elion.elion_laylout.MainActivityViewPagerAdapter;
import elion.elion_laylout.NoSlidingViewPaper;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private NoSlidingViewPaper mpager;
    private MainActivityViewPagerAdapter mainActivityViewPagerAdapter;
    private NavigationView navigationView;
    private BottomNavigationView navigation;
    private Toolbar toolbar;
    private DrawerLayout drawer;
    private CoordinatorLayout right;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.bt_nav_camera:
                    mpager.setCurrentItem(0);
                    return true;
                case R.id.bt_nav_gallery:
                    mpager.setCurrentItem(1);
                    return true;
                case R.id.bt_nav_slideshow:
                    mpager.setCurrentItem(2);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();
        init();

    }

    public void findView() {
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        mpager = (NoSlidingViewPaper) findViewById(R.id.vp_main_container);
        right = (CoordinatorLayout) findViewById(R.id.cl_right);
    }

    public void init() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //获取屏幕的宽高
                WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
                Display display = manager.getDefaultDisplay();
                //设置右面的布局位置  根据左面菜单的right作为右面布局的left   左面的right+屏幕的宽度（或者right的宽度这里是相等的）为右面布局的right
                right.layout(navigationView.getRight(), 0, navigationView.getRight() + display.getWidth(), display.getHeight());
            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });

        mainActivityViewPagerAdapter = new MainActivityViewPagerAdapter(getSupportFragmentManager());
//        为Adapter添加Fragment
        mainActivityViewPagerAdapter.addFragment(new SelectFragment());
        mainActivityViewPagerAdapter.addFragment(new ServiceFragment());
        mainActivityViewPagerAdapter.addFragment(new WarningFragment());
        mpager.setAdapter(mainActivityViewPagerAdapter);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

//        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings)
//        {
//            return true;
//        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        Intent intent = null;
        if (id == R.id.nav_seed) {
            intent = new Intent(MainActivity.this, SeedActivity.class);
        } else if (id == R.id.nav_send) {
            intent = new Intent(MainActivity.this, Me_ContectActivity.class);
        } else if (id == R.id.nav_about) {
            intent = new Intent(MainActivity.this, Me_AboutActivity.class);
        }
        if (intent != null) {
            startActivity(intent);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


}